package com.googlecode.gwtbb.client.only.widget.basic;

import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.AbsolutePanel;
import com.google.gwt.user.client.ui.Widget;

/**
 * @author Abraham Grief
 * @version $Id$
 * @since Jan 26, 2009
 * @deprecated currently doesn't work
 */
public class FlexDecorator extends Composite {
	private FlexTable flexTable;

	public FlexDecorator() {
		flexTable = new FlexTable();
		initWidget(flexTable);
		flexTable.setStylePrimaryName("gwtbb-FlexDecorator");
		flexTable.setCellPadding(0);
		flexTable.setCellSpacing(0);
	}

	public void addCell(int row, int col) {
		// todo maybe a spacer would work... continue making speech bubble and this
		AbsolutePanel cell = new AbsolutePanel();
		flexTable.setWidget(row, col, cell);
		cell.setStylePrimaryName("cell-" + row + "-" + col);
	}

	public void setWidth(int row, int col, String width) {
		flexTable.getWidget(row, col).setWidth(width);
		flexTable.getFlexCellFormatter().setWidth(row, col, width);
	}

	public void setHeight(int row, int col, String height) {
		flexTable.getWidget(row, col).setHeight(height);
		flexTable.getFlexCellFormatter().setHeight(row, col, height);
	}

	public void addCell(int row, int col, String width, String height) {
		addCell(row, col);
		setWidth(width);
		setHeight(height);
	}

	public void addCell(int row, int col, int rowSpan, int colSpan, String width, String height) {
		addCell(row, col, width, height);
		flexTable.getFlexCellFormatter().setRowSpan(row, col, rowSpan);
		flexTable.getFlexCellFormatter().setColSpan(row, col, colSpan);
	}

	public void setMainWidget(Widget widget) {
		flexTable.setWidget(1, 1, widget);
	}

/*
This was the old implementation of FlexDecorator, based on DecoratorPanel.
It was changed to this slower way using panels because of two problems with this implementation:
1. doing width=100% will crush the left and right decorations
2. in IE, mixing absolute widths with auto or percentage widths on individual tds does not work.

	private static final String DEFAULT_STYLENAME = "gwtbb-FlexDecorator";
	private Element containerElement;
	private Element tbody;

	public FlexDecorator(int numRows, int numCols) {
		super(DOM.createTable());

		if (numRows < 3)
			throw new IllegalArgumentException("numRows must be at least 3, but it was: " + numRows);
		if (numCols < 3)
			throw new IllegalArgumentException("numCols must be at least 3, but it was: " + numCols);

		// Add a tbody
		Element table = getElement();
		tbody = DOM.createTBody();
		DOM.appendChild(table, tbody);
		DOM.setElementPropertyInt(table, "cellSpacing", 0);
		DOM.setElementPropertyInt(table, "cellPadding", 0);

		List<String> colStyles = new ArrayList<String>(numCols);
		if (LocaleInfo.getCurrentLocale().isRTL()) {
			for (int i = numCols - 1; i >= 0; i--)
				colStyles.add(String.valueOf(i));
		} else {
			for (int i = 0; i < numCols; i++)
				colStyles.add(String.valueOf(i));
		}

		// the first row
		Element row = DOM.createTR();
		for (String style : colStyles)
			DOM.appendChild(row, createTD("cell-0-" + style));
		DOM.appendChild(tbody, row);

		// the second row... contains the main widget
		row = DOM.createTR();
		DOM.appendChild(row, createTD("cell-1-" + colStyles.get(0)));
		Element mainTD = createTD("cell-1-1");
		DOM.appendChild(row, mainTD);
		DOM.setElementPropertyInt(mainTD, "colSpan", numCols - 2);
		DOM.setElementPropertyInt(mainTD, "rowSpan", numRows - 2);
		DOM.appendChild(row, createTD("cell-1-" + colStyles.get(colStyles.size() - 1)));
		containerElement = DOM.getFirstChild(mainTD);
		DOM.appendChild(tbody, row);

		// filler rows, if there are any
		for (int i = 2; i < numRows - 1; i++) {
			row = DOM.createTR();
			DOM.appendChild(row, createTD("cell-" + i + "-" + colStyles.get(0)));
			DOM.appendChild(row, createTD("cell-" + i + "-" + colStyles.get(colStyles.size() - 1)));
			DOM.appendChild(tbody, row);
		}

		// the last row
		row = DOM.createTR();
		for (String style : colStyles)
			DOM.appendChild(row, createTD("cell-" + ((numRows - 1) + "-" + style)));
		DOM.appendChild(tbody, row);

		// Set the overall style name
		setStyleName(DEFAULT_STYLENAME);
	}

	@Override
	protected Element getContainerElement() {
		return containerElement;
	}

	protected Element getCellElement(int row, int cell) {
		Element tr = DOM.getChild(tbody, row);
		Element td = DOM.getChild(tr, cell);
		return DOM.getFirstChild(td);
	}

	private static Element createTD(String styleName) {
		Element tdElem = DOM.createTD();
		Element inner = DOM.createDiv();
		DOM.appendChild(tdElem, inner);
		setStyleName(tdElem, styleName);
		setStyleName(inner, styleName + "-inner");
		return tdElem;
	}
*/
}
